package cn.mes.module.mes.controller.admin.basetimeperiod;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.mes.framework.common.pojo.PageParam;
import cn.mes.framework.common.pojo.PageResult;
import cn.mes.framework.common.pojo.CommonResult;
import cn.mes.framework.common.util.object.BeanUtils;
import static cn.mes.framework.common.pojo.CommonResult.success;

import cn.mes.framework.excel.core.util.ExcelUtils;

import cn.mes.framework.apilog.core.annotation.ApiAccessLog;
import static cn.mes.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.mes.module.mes.controller.admin.basetimeperiod.vo.*;
import cn.mes.module.mes.dal.dataobject.basetimeperiod.BaseTimePeriodDO;
import cn.mes.module.mes.service.basetimeperiod.BaseTimePeriodService;

@Tag(name = "管理后台 - 时间段配置信息")
@RestController
@RequestMapping("/mes/base-time-period")
@Validated
public class BaseTimePeriodController {

    @Resource
    private BaseTimePeriodService baseTimePeriodService;

    @PostMapping("/create")
    @Operation(summary = "创建时间段配置信息")
    @PreAuthorize("@ss.hasPermission('mes:base-time-period:create')")
    public CommonResult<String> createBaseTimePeriod(@Valid @RequestBody BaseTimePeriodSaveReqVO createReqVO) {
        return success(baseTimePeriodService.createBaseTimePeriod(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新时间段配置信息")
    @PreAuthorize("@ss.hasPermission('mes:base-time-period:update')")
    public CommonResult<Boolean> updateBaseTimePeriod(@Valid @RequestBody BaseTimePeriodSaveReqVO updateReqVO) {
        baseTimePeriodService.updateBaseTimePeriod(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除时间段配置信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:base-time-period:delete')")
    public CommonResult<Boolean> deleteBaseTimePeriod(@RequestParam("id") String id) {
        baseTimePeriodService.deleteBaseTimePeriod(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得时间段配置信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:base-time-period:query')")
    public CommonResult<BaseTimePeriodDO> getBaseTimePeriod(@RequestParam("id") String id) {
        BaseTimePeriodDO baseTimePeriod = baseTimePeriodService.getBaseTimePeriod(id);
        return success(baseTimePeriod);
    }

    @GetMapping("/page")
    @Operation(summary = "获得时间段配置信息分页")
    @PreAuthorize("@ss.hasPermission('mes:base-time-period:query')")
    public CommonResult<PageResult<BaseTimePeriodDO>> getBaseTimePeriodPage(@Valid BaseTimePeriodPageReqVO pageReqVO) {
        PageResult<BaseTimePeriodDO> pageResult = baseTimePeriodService.getBaseTimePeriodPage(pageReqVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出时间段配置信息 Excel")
    @PreAuthorize("@ss.hasPermission('mes:base-time-period:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportBaseTimePeriodExcel(@Valid BaseTimePeriodPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<BaseTimePeriodDO> list = baseTimePeriodService.getBaseTimePeriodPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "时间段配置信息.xls", "数据", BaseTimePeriodRespVO.class,
                        BeanUtils.toBean(list, BaseTimePeriodRespVO.class));
    }

}